package com.hc;

import java.sql.*;

public class DBOperater {
    public static void main(String[] args) {
//        JDBCOperate();
        DBCPSelect();
    }

    public static void JDBCOperate(){
        try {
            //1.注册驱动
            Class.forName(PropertiesUtil.getDriverClassName());
            //2.建立连接
            Connection conn = DriverManager.getConnection(PropertiesUtil.getUrl(),
                                            PropertiesUtil.getUsername(),PropertiesUtil.getPassword());
            //3.使用Statement执行sql语句
            Statement statement = conn.createStatement();
            String sql = "select * from emp";
            ResultSet resultSet = statement.executeQuery(sql);
            //4.处理结果
            while (resultSet.next()){
                String eid = resultSet.getString("eid");
                String ename = resultSet.getString("ename");
                String dept_name = resultSet.getString("dept_name");
                System.out.println("eid="+eid+",ename="+ename+",dept_name="+dept_name);
            }
            //5.关闭连接
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void DBCPSelect(){
        try {
            //1.获取连接对象
            Connection connection = DBCPUtil.getConnection();
            //2.定义sql
            String sql = "select * from emp where ename like ?";
            //3.创建预编译对象
            PreparedStatement statement = connection.prepareStatement(sql);
            //4.设置占位符的值
            statement.setString(1,"%张%");
            //5.执行sql
            ResultSet resultSet = statement.executeQuery();
            //6.处理结果
            while (resultSet.next()){
                String eid = resultSet.getString("eid");
                String ename = resultSet.getString("ename");
                String dept_name = resultSet.getString("dept_name");
                System.out.println("eid="+eid+",ename="+ename+",dept_name="+dept_name);
            }
            //7.关闭连接
            DBCPUtil.close(connection,resultSet,statement);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    //Statement批处理
    public static void statementBatch(){
        Statement statement = null;
        try {
            Connection connection = DBCPUtil.getConnection();
            String sql1 = "insert into emp values(30,'张三','男',6000,'2022-02-10','市场部')";
            String sql2 = "insert into emp values(31,'张三','男',6000,'2022-02-10','市场部')";
            String sql3 = "insert into emp values(32,'张三','男',6000,'2022-02-10','市场部')";
            statement = connection.createStatement();
            statement.addBatch(sql1);
            statement.addBatch(sql2);
            statement.addBatch(sql3);
            statement.executeBatch();
        } catch (SQLException e) {
            try {
                if(statement != null){
                    statement.clearBatch();
                }
            } catch (SQLException ex) {

            }
        }
    }

    //PreparedStatement批处理
    public static void prepardStatementBatch(){
        PreparedStatement preparedStatement = null;
        try {
            Connection connection = DBCPUtil.getConnection();
            String sql = "insert into emp values (?,?,'男',6000,'2022-02-10','市场部')";
            preparedStatement = connection.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);//返回自增主键
            for (int i = 40; i < 50; i++) {
                preparedStatement.setInt(1,i);
                preparedStatement.setString(2,"名称"+i);
                preparedStatement.addBatch();
            }
            preparedStatement.executeBatch();
            ResultSet resultSet = preparedStatement.getGeneratedKeys();
        }catch (Exception e){
            try {
                if(preparedStatement != null){
                    preparedStatement.clearBatch();
                }
            } catch (SQLException ex) {

            }
        }
    }

    //事务
    public static void transcation(){
        Connection connection = null;
        try {
            connection = DBCPUtil.getConnection();
            connection.setAutoCommit(false);
            String sql1 = "update emp set ename='张三' where eid = 1";
            Statement statement = connection.createStatement();
            statement.executeUpdate(sql1);
            connection.commit();
        } catch (Exception e) {
            try {
                connection.rollback();
            } catch (SQLException ex) {
            }
        }
    }
}
